package org.adream.account.controller;

import java.util.List;
import java.util.Map;

import org.adream.account.aop.annotation.MonitorRequest;
import org.adream.account.dao.RoleResLinkDao;
import org.adream.account.entity.ResourceEntity;
import org.adream.account.entity.RoleEntity;
import org.adream.account.entity.RoleResLinkEntity;
import org.adream.account.service.ResourceService;
import org.adream.account.service.RoleService;
import org.adream.account.util.Constant;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 角色管理模块相关页面
 * @author sl
 *
 */
@Controller
public class RoleController {
	
	private final static Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private ResourceService resourceService;
	
	@Autowired
	private RoleResLinkDao roleResDao;
	
	//角色列表页面
	@RequestMapping(value = "/rolePage")
	public ModelAndView role() {
		ModelAndView mv = new ModelAndView("/role/role");
		return mv;
	}
	
	//角色添加页面
	@RequestMapping(value = "/roleAddPage")
	@MonitorRequest
	public ModelAndView roleAdd() {
		ModelAndView mv = new ModelAndView("/role/role_add");
		return mv;
	}
	
	//角色编辑页面
	@RequestMapping(value = "/roleEditPage")
	@MonitorRequest
	public ModelAndView roleEdit(String rleId) {
		ModelAndView mv = new ModelAndView("/role/role_edit");
		
		RoleEntity role = roleService.queryRoleByRleId(rleId);
		//查询角色对应的相应资源
		List<ResourceEntity> resources = resourceService.queryResourcesByDr(Constant.ONE);
		Map<String,Object> map = mv.getModel();
		if(!Utils.isEmptyCollection(resources)) {			
			int[] reses = new int[resources.size()];
			int size = resources.size();
			//构建数组  用于资源选项
			for(int i = 0;i < size;i++) {
				RoleResLinkEntity roleRes = null;
				roleRes = roleResDao.queryRoleResByIdAndDr(rleId, resources.get(i).getResId(),Constant.ONE);
				if(roleRes != null) {
					reses[i] = Constant.ONE;
				}
			}
			map.put("reses",reses);
		}
		map.put("role", role);
		map.put("resources", resources);
		return mv;
	}
}